package cmpe451.group7.ourcity.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * Entity class for places.
 */
@Entity
@Table(name = "comment")
public class Comment implements Serializable {

	private static final long serialVersionUID = -3321356183696542360L;

	private long commentID;
	private Place place;
	private User user;
	private Date date;
	private String comment;
	private Set<Tag> tags = new HashSet<Tag>();
	private Set<Multimedia> multimedia = new HashSet<Multimedia>();

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getCommentID() {
		return commentID;
	}

	public void setCommentID(long commentID) {
		this.commentID = commentID;
	}

	@ManyToOne
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		if (date != null)
			this.date = new Date(date.getTime());
		else
			this.date = null;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@ManyToOne
	public Place getPlace() {
		return place;
	}

	public void setPlace(Place place) {
		this.place = place;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "comment_tag", joinColumns = { @JoinColumn(name = "commentID") }, inverseJoinColumns = { @JoinColumn(name = "tagID") })
	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public void addTag(Tag t) {
		tags.add(t);
	}

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "COMMENT_ID")
	public Set<Multimedia> getMultimedia() {
		return multimedia;
	}

	public void setMultimedia(Set<Multimedia> multimedia) {
		this.multimedia = multimedia;
	}
	
	public void addMultimedia(Multimedia item) {
		multimedia.add(item);
	}

	@Transient
	public String getCSVTags() {
		String res = "";
		for (Iterator<Tag> iterator = tags.iterator(); iterator.hasNext();) {
			Tag t = (Tag) iterator.next();
			res += t.getTagName() + ", ";
		}

		return res.substring(0, Math.max(0, res.length() - 2));
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (commentID ^ (commentID >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Comment))
			return false;
		Comment other = (Comment) obj;
		if (commentID != other.commentID)
			return false;
		return true;
	}

}
